#include "formplay.h"
#include "ui_formplay.h"

#include <QDebug>

FormPlay::FormPlay(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormPlay)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() | Qt::Dialog);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose);

    mTimer = new QTimer();
    mTimer->setSingleShot(false);
    mTimer->setInterval(500);

    QList<QString> plists;
    mDbopt = new DCSDbOperator();
    mDbopt->getPlayLists(plists);
    mPlayer = new DCSPlayer();
    mPlayer->setPlayLists(plists);
    connect(mPlayer, &DCSPlayer::playStateChanged, this, &FormPlay::on_playStateChanged);
    connect(mPlayer, &DCSPlayer::playVideoPrepared, this, &FormPlay::on_playVideoPrepared);
    connect(mTimer, &QTimer::timeout, this, &FormPlay::on_RefreshPlayProgress);


    mPlayer->playerStart();
    mTimer->start();
    qDebug() << "[FormPlay] \t Constructor";
}

FormPlay::~FormPlay()
{
    delete mDbopt;
    delete mPlayer;
    delete ui;

    qDebug() << "[FormPlay] \t Destructor";
}

void FormPlay::on_pushButtonPlay_clicked()
{
    if (mPlayer->state() == DCSPlayer::Play)
    {
        mPlayer->playerPause();
    }
    else if (mPlayer->state() == DCSPlayer::Stop)
    {
        mPlayer->playerStart();
    }
    else if (mPlayer->state() == DCSPlayer::Pause)
    {
        mPlayer->playerContinue();
    }
}

void FormPlay::on_pushButtonStop_clicked()
{
    mPlayer->playerStop();
}

void FormPlay::on_pushButtonPrev_clicked()
{
    mPlayer->playerPrev();
}

void FormPlay::on_pushButtonNext_clicked()
{
    mPlayer->playerNext();
}

void FormPlay::on_pushButtonReturn_clicked()
{
    close();
}

void FormPlay::on_playStateChanged(quint8 state)
{
    qDebug() << "play state:" << state;

    switch (state)
    {
        case DCSPlayer::Play:
            ui->pushButtonPlay->setText(tr("暂停"));
            break;

        case DCSPlayer::Pause:
        case DCSPlayer::Stop:
            ui->pushButtonPlay->setText(tr("播放"));
            break;
        default:
            break;
    }
}

void FormPlay::on_playVideoPrepared(quint8 index)
{
    ui->labelVideoName->setText(mPlayer->getCurrentVideoName());
}

void FormPlay::on_RefreshPlayProgress()
{
    ui->progressBar->setValue(mPlayer->playProgress());
}
